//
//  ViewController.m
//  mvvm
//
//  Created by zhtc-mac1 on 2017/11/29.
//  Copyright © 2017年 lixiaoyun. All rights reserved.
//

#import "ViewController.h"
#import "HomeViewModel.h"
#import "HomeViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic, strong)HomeViewModel *viewModel;

@end

#define KScreenWidth [UIScreen mainScreen].bounds.size.width
#define KScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setUpUI];
    [self requestNetData];
}

- (void)requestNetData
{
    _viewModel = [HomeViewModel new];
    
    [_viewModel.requestSignal subscribeNext:^(NSArray * datasource) {
        [self.tableView reloadData];
        
    } error:^(NSError * _Nullable error) {
        
        
    }];
}

- (void)setUpUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight - 64) style:UITableViewStylePlain];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = YES;
    _tableView.backgroundColor = [UIColor whiteColor];
    [_tableView registerClass:[HomeViewCell class] forCellReuseIdentifier:@"TESTCELLID"];
    [self.view addSubview:_tableView];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataSource.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TESTCELLID"];
    cell.model = self.viewModel.dataSource[indexPath.row];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
